package com.centaline.androidsalesblog.base;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.PopupWindow;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.RentDetailActivity;
import com.centaline.androidsalesblog.activities.sale.SaleDetailActivity;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.DefaultHint;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BottomSheetStatus;
import com.centaline.androidsalesblog.iml.DropCompleteListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.businissobject.PostMapBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.model.databaseobject.SearchDo;
import com.centaline.androidsalesblog.utils.DropUtil;
import com.centaline.androidsalesblog.widgets.PostMapPop;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;

import java.util.ArrayList;

/**
 * 租售列表\地图双模式基类
 * Created by guilin on 16/2/18.
 */
public abstract class BasePostListMapActivity extends BaseListMapActivity {

    protected PostListApi postListApi;

    protected DropCompleteListener dropCompleteListener = new DropCompleteListener() {
        @Override
        public void complete(int position, boolean fromMore, int type, DropBo... dropBos) {
            final int size = dropBos == null ? 0 : dropBos.length;
            if (fromMore) {
                //来源于更多
                if (size == 0) {
                    postListApi.resetOther();
                    resetTab(3);
                    downRefresh(false);
                } else {
                    setTab(3, "更多");
                    postListApi.resetOther();
                    final StringBuilder feature = new StringBuilder();
                    for (DropBo dropBo : dropBos) {
                        final int innerType = dropBo.getType();
                        switch (innerType) {
                            case 100://排序
                                postListApi.setSort(dropBo.getValue());
                                break;
                            case 110://住宅类型
                                postListApi.setProperty(dropBo.getValue());
                                break;
                            case 60://面积
                                postListApi.setArea(dropBo.getValue());
                                break;
                            case 70://装修
                                postListApi.setFitment(dropBo.getValue());
                                break;
                            case 90://建造年代
                                postListApi.setConstruction(dropBo.getValue());
                                break;
                            case 50://朝向
                                postListApi.setDirection(dropBo.getValue());
                                break;
                            case 80:
                                feature.append(dropBo.getValue());
                                feature.append("_");
                                break;
                            default:
                                break;
                        }
                    }
                    if (feature.toString().contains("_")) {
                        feature.deleteCharAt(feature.length() - 1);
                    }
                    postListApi.setFeature(feature.toString());
                    baiduEvent("C1_8_Value");
                    downRefresh(false);
                }
            } else {
                switch (type) {
                    case 10://区域
                        postListApi.reset();
                        postListApi.setType(getDefaultListType());
                        setViewType(0);
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                                setTab(position, "区域");
                            } else {
                                postListApi.setRegion(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_5_Value", dropBos[0].getText());
                            }
                            downRefresh(false, true);
                        } else if (size == 2) {
                            postListApi.setRegion(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[1])) {
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_5_Value", dropBos[0].getText());
                            } else {
                                postListApi.setScopeId(dropBos[1].getValue());
                                setTab(position, dropBos[1].getText());
                                baiduEvent("C1_5_Value", dropBos[1].getText());
                            }
                            downRefresh(false, true);
                        }
                        break;
                    case 11://附近
                        postListApi.reset();
                        postListApi.setType(getDefaultListType());
                        setViewType(0);
                        if (size == 2) {
                            postListApi.setRound(dropBos[1].getValue());
                            setTab(position, dropBos[1].getText());
                            downRefresh(true);
                            baiduEvent("C1_5_Value", dropBos[1].getText());
                        }
                        break;
                    case 20://地铁
                        postListApi.reset();
                        postListApi.setType(getDefaultListType());
                        setViewType(1);
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                                postListApi.setType("metro");
                                setTab(position, "地铁");
                            } else {
                                postListApi.setSubwayId(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_5_Value", dropBos[0].getText());
                            }
                            downRefresh(false, true);
                        } else if (size == 2) {
                            postListApi.setSubwayId(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[1])) {
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_5_Value", dropBos[0].getText());
                            } else {
                                postListApi.setStationId(dropBos[1].getValue());
                                setTab(position, dropBos[1].getText());
                                baiduEvent("C1_5_Value", dropBos[1].getText());
                            }
                            downRefresh(false, true);
                        }
                        break;
                    case 30://总价
                        postListApi.setPrice("");
                        postListApi.setMin("");
                        postListApi.setMax("");
                        if (size == 1) {
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                postListApi.setPrice(dropBos[0].getValue());
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_6_Value", dropBos[0].getText());
                            }
                            downRefresh(false);
                        } else if (size == 2) {
                            postListApi.setPrice("");
                            postListApi.setMin(dropBos[0].getValue());
                            postListApi.setMax(dropBos[1].getValue());
                            setTab(position, DropUtil.customPrice(dropBos[0], dropBos[1]));
                            downRefresh(false);
                            baiduEvent("C1_6_Value", DropUtil.customPrice(dropBos[0], dropBos[1]));
                        }
                        break;
                    case 40://房型
                        if (size == 0) {
                            postListApi.setRoom("");
                            resetTab(position);
                        } else {
                            postListApi.setRoom(dropBos[0].getValue());
                            if (DropUtil.compareValue(dropBos[0])) {
                                resetTab(position);
                            } else {
                                setTab(position, dropBos[0].getText());
                                baiduEvent("C1_7_Value", dropBos[0].getText());
                            }
                        }
                        downRefresh(false);
                        break;
                    default:
                        break;
                }
            }
        }
    };

    protected PostMapPop postMapPop;
    protected ArrayList<PostDo> popList = new ArrayList<>();//显示数据

    protected IRecycleViewListener iRecycleViewListener = new IRecycleViewListener() {
        @Override
        public void downRefresh() {
            postMapPop.stopRefresh(false);
        }

        @Override
        public void upRefresh() {
            postMapPop.stopRefresh(false);
        }

        @Override
        public void onItemClick(int position) {
            popItemCLick(position);
        }
    };

    private PopupWindow.OnDismissListener onDismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            popDismiss(true);
        }
    };

    protected BottomSheetStatus<PostMapBo> bottomSheetStatus = new BottomSheetStatus<PostMapBo>() {
        @Override
        public void bottomSheetCallback(PostMapBo postMapBo, int status) {
            popList.clear();
            popList.addAll(postMapBo.getPostDos());
            postMapPop.stopRefresh(false);
            postMapPop.show(postMapBo.getCestName());
            popDismiss(false);
        }
    };

    protected void popItemCLick(int position) {
        if ("s".equalsIgnoreCase(popList.get(position).getPostType())) {
            startActivity(new Intent(this, SaleDetailActivity.class)
                    .putExtra(ExtraConstant.PostId, popList.get(position).getPostId()));
        } else {
            startActivity(new Intent(this, RentDetailActivity.class)
                    .putExtra(ExtraConstant.PostId, popList.get(position).getPostId()));
        }
    }

    protected void popDismiss(boolean reset) {

    }

    /**
     * 设置默认请求类型
     */
    protected String getDefaultListType() {
        return "";
    }

    /**
     * 设置列表视图
     */
    protected void setViewType(int viewType) {

    }

    @Override
    protected void initViews() {
        dropDownView.setDropCompleteListener(dropCompleteListener);
        postMapPop = new PostMapPop(this, findViewById(R.id.coordinatorLayout), popList,
                GlideLoad.init(this), onDismissListener, iRecycleViewListener);
        initApi();
        super.initViews();
        setFromSearch(getIntent(), false);
    }

    @Override
    protected DefaultHint getDefaultHint() {
        return DefaultHint.SALE;
    }

    /**
     * 初始化api
     */
    protected abstract void initApi();

    /**
     * 请求列表
     */
    protected abstract void requestList();

    /**
     * 重置标签
     *
     * @param position 位置
     */
    protected void resetTab(int position) {
        dropDownView.resetTab(position);
    }

    /**
     * 设置标签
     *
     * @param position 位置
     * @param tab      文本
     */
    protected void setTab(int position, String tab) {
        dropDownView.setTab(position, tab);
    }

    /**
     * 搜索页面传参
     *
     * @param request 请求
     */
    protected void setFromSearch(Intent intent, boolean request) {
        if (intent != null) {
            SearchDo searchDo = intent.getParcelableExtra(ExtraConstant.SearchObject);
            if (searchDo == null) {
                String query = intent.getStringExtra(ExtraConstant.SearchContent);
                if (!TextUtils.isEmpty(query)) {
                    postListApi.setKeyword(query);
                    clearType = 1;
                    setSearchContent(query);
                    if (request) {
                        downRefresh(false);
                    }
                }
            } else {
                String category = searchDo.getCategory();
                clearType = 0;
                postListApi.setKeyword("");
                if (!TextUtils.isEmpty(category)) {
                    if (category.startsWith("est")) {
                        postListApi.setRegion("");
                        postListApi.setEstCode("");
                        setSearchContent(searchDo.getTitle());
                        postListApi.setEstCode(searchDo.getCode());
                    } else if ("region".equalsIgnoreCase(category)) {
                        postListApi.setRegion("");
                        postListApi.setEstCode("");
                        postListApi.setRegion(searchDo.getCode());
                        setTab(0, searchDo.getTitle());
                    } else if ("block".equalsIgnoreCase(category)) {
                        postListApi.setRegion("");
                        postListApi.setEstCode("");
                        postListApi.setScopeId(searchDo.getCode());
                        setTab(0, searchDo.getTitle());
                    } else if ("key".equals(category)) {
                        clearType = 1;
                        setSearchContent(searchDo.getTitle());
                        postListApi.setKeyword(searchDo.getTitle());
                    }
                    if (request) {
                        downRefresh(false);
                    }
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            switch (requestCode) {
                case CommonConstant.REQ_SEARCH:
                    setFromSearch(data, true);
                    break;
                default:
                    break;
            }
        }
    }
}
